package cn.topcodes.tcsf.app.web.controller.userend.webim;

import cn.topcodes.tcsf.app.domain.entity.AppUser;
import cn.topcodes.tcsf.app.service.AppUserService;
import cn.topcodes.tcsf.app.web.controller.userend.webim.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Unicorn on 2018/3/10.
 */
@RestController
@RequestMapping("/userend/webim")
public class WebIMController {

    @Autowired
    private AppUserService appUserService;

    @RequestMapping(value = "init-info", method = RequestMethod.GET)
    public LayIMStandardResponse initInfo(HttpSession session) {
        String token = (String) session.getAttribute("token");
        AppUser appUser = appUserService.findByToken(token);

        LayIMStandardResponse r = new LayIMStandardResponse();
        r.setCode(0);
        r.setMsg("Get success");
        InitInfo initInfo = new InitInfo();

        IMPerson mine = new IMPerson();
        mine.setId(appUser.getId());
        mine.setAvatar(appUser.getHeadImgUrl());
        mine.setStatus(IMPerson.STATUS_ONLINE);
        mine.setUsername(appUser.getRealName());
        initInfo.setMine(mine);

        List<Group> friendGroups = new ArrayList<>();
        Group group = new Group();
        group.setGroupname("Default group");
        group.setId(1L);

        List<Friend> friends = new ArrayList<>();
        List<AppUser> appUsers = this.appUserService.findAll();
        for(AppUser mAppUser : appUsers) {
            Friend friend = new Friend();
            friend.setId(mAppUser.getId());
            friend.setAvatar(mAppUser.getHeadImgUrl());
            friend.setStatus(IMPerson.STATUS_OFFLINE);
            friend.setUsername(mAppUser.getRealName());
            friends.add(friend);
        }

        group.setList(friends);
        friendGroups.add(group);
        initInfo.setFriend(friendGroups);

        r.setData(initInfo);
        return r;
    }

    @RequestMapping(value = "members", method = RequestMethod.GET)
    public LayIMStandardResponse members() {

        return null;
    }
}
